home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////////
- // Pocket PC Game Programming
- // Chapter 9: Sprites and Animation
- //
- // CSprite Source File
- //
- // This file includes the CSprite class implementation.
- //
- //////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "CBitmap.h"
- #include "CSprite.h"
-
-
- //////////////////////////////////////////////////////////////////////
- // CSprite Constructor
- //
- // This function is called when the class is instantiated.
- //
- CSprite::CSprite():CBitmap()
- {
- Init();
- }
-
- //////////////////////////////////////////////////////////////////////
- // CSprite Constructor (Overloaded)
- //
- // This function is called when the class is instantiated.
- //
- CSprite::CSprite(HDC hdc):CBitmap(hdc)
- {
- Init();
- }
-
- void CSprite::Init()
- {
- X_Loc = 0;
- Y_Loc = 0;
- X_Dir = 1;
- Y_Dir = 1;
- X_Spd = 1;
- Y_Spd = 1;
-
- bUnderSaved = FALSE;
- bAlive = FALSE;
-
- DestRect = new RECT;
- Rect1 = new RECT;
- Rect2 = new RECT;
- }
-
-
- //////////////////////////////////////////////////////////////////////
- // CSprite Destructor
- //
- // This function is called when the class is terminated.
- //
- CSprite::~CSprite()
- {
- DeleteDC(hdcUnder);
-
- delete DestRect;
- delete Rect1;
- delete Rect2;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CSprite::SaveUnder
- //
- // Saves the background image under the sprite.
- //
- BOOL CSprite::SaveUnder(HDC hdc)
- {
- if (!bUnderSaved)
- {
- hUnder = CreateCompatibleBitmap(hdc, ImageWidth(), ImageHeight());
- if (hUnder == NULL)
- return FALSE;
-
- hdcUnder = CreateCompatibleDC(hdc);
- if (hdcUnder == NULL)
- return FALSE;
-
- SelectObject(hdcUnder, hUnder);
- DeleteObject(hUnder);
- bUnderSaved = TRUE;
- }
-
- if (hdcUnder != 0 && hdc != 0)
- BitBlt(hdcUnder, 0, 0, ImageWidth(), ImageHeight(), hdc, GetX(), GetY(), SRCCOPY);
-
- return TRUE;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CSprite::RestoreUnder
- //
- // Restores the background image that was under the sprite.
- //
- BOOL CSprite::RestoreUnder(HDC hdc)
- {
- if (hdcUnder != 0 && hdc != 0)
- BitBlt(hdc, GetX(), GetY(), ImageWidth(), ImageHeight(), hdcUnder, 0, 0, SRCCOPY);
- else
- return FALSE;
-
- return TRUE;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CSprite::TransBlit
- //
- // Draws the sprite transparently.
- //
- BOOL CSprite::TransBlit(COLORREF clrTrans)
- {
- return CBitmap::TransBlit(GetX(), GetY(), clrTrans);
- }
-
- //////////////////////////////////////////////////////////////////////
- // CSprite::BitBlit
- //
- // Draws the sprite to the destination DC already specified.
- //
- BOOL CSprite::BitBlit()
- {
- return CBitmap::BitBlit(GetX(), GetY());
- }
-
- //////////////////////////////////////////////////////////////////////
- // CSprite::BitBlit (Overloaded)
- //
- // Draws the sprite to the destination DC parameter.
- //
- BOOL CSprite::BitBlit(HDC hdc)
- {
- return CBitmap::BitBlit(hdc, GetX(), GetY());
- }
-
- //////////////////////////////////////////////////////////////////////
- // CSprite::StretchBlit
- //
- // Draws a scaled sprite to the destination.
- //
- BOOL CSprite::StretchBlit(int dx, int dy)
- {
- return CBitmap::StretchBlit(GetX(), GetY(), dx, dy);
- }
-
- //////////////////////////////////////////////////////////////////////
- // CSprite::Intersected
- //
- // Checks to see if this sprite has collided with another.
- // The POINT struct is returned with the center point of the
- // bounding rectangle that formed the union of the two sprites.
- //
- BOOL CSprite::Intersected(CSprite *Sprite, POINT &pt)
- {
- RECT rt;
-
- if (Intersected(Sprite, rt))
- {
- pt.x = (rt.left + rt.right) / 2;
- pt.y = (rt.top + rt.bottom) / 2;
- return TRUE;
- }
-
- return FALSE;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CSprite::Intersected
- //
- // Checks to see if this sprite has collided with another.
- // This version returns the actual union rectangle.
- //
- BOOL CSprite::Intersected(CSprite *Sprite, RECT &rt)
- {
- int iShrinkX, iShrinkY;
-
- iShrinkX = ImageWidth() >> 2;
- iShrinkY = ImageHeight() >> 2;
-
- Rect1->left = GetX() + iShrinkX;
- Rect1->top = GetY() + iShrinkY;
- Rect1->right = GetX() + ImageWidth() - iShrinkX;
- Rect1->bottom = GetY() + ImageHeight() - iShrinkY;
-
- iShrinkX = Sprite->ImageWidth() >> 2;
- iShrinkY = Sprite->ImageHeight() >> 2;
-
- Rect2->left = Sprite->GetX() + iShrinkX;
- Rect2->top = Sprite->GetY() + iShrinkY;
- Rect2->right = Sprite->GetX() + Sprite->ImageWidth() - iShrinkX;
- Rect2->bottom = Sprite->GetY() + Sprite->ImageHeight() - iShrinkY;
-
- if (IntersectRect(DestRect, Rect1, Rect2))
- {
- rt.left = DestRect->left;
- rt.right = DestRect->right;
- rt.top = DestRect->top;
- rt.bottom = DestRect->bottom;
- return TRUE;
- }
-
- return FALSE;
- }
-
-
-